import Vue from 'vue'
import Router from 'vue-router'
import Login from '../views/Login'
import SignUp from '../views/SignUp'
import NoAuth from '../views/NoAuth'
import Manage from '../views/Manage'
import cookies from 'js-cookie'
import RoleManage from "../views/RoleManage";
import PermissionManage from "../views/PermissionManage";
import Index from  "../views/Index";
import VideoDetail from "../views/VideoDetail";
import VideoAudit from "../views/VideoAudit";
import PersonalCenter from "../views/PersonalCenter";
import PersonalInfoComponent from "../components/PersonalInfoComponent";
import VideoManageComponent from "../components/VideoManageComponent";
import VideoTransProgressComponent from "../components/VideoTransProgressComponent";
import CollectionManageComponent from "../components/CollectionManageComponent";
Vue.use(Router)

const router = new Router({
  mode: 'history',
  routes: [{
      //主页重定向到login，后面改为main
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      name: 'login',
      component: Login
    },
    {
      path: '/signUp',
      name: 'signUp',
      component: SignUp
    },
    {
      path: '/index',
      name: 'index',
      component: Index
    },
    {
      path: '/videoDetail',
      name: 'videoDetail',
      component: VideoDetail
    },
    {
      path: '/manage',
      name: 'manage',
      component: Manage,
      children: [
        {
        path: 'roleManage',
        name: 'roleManage',
        component: RoleManage
        },
        {
          path: 'permissionManage',
          name: 'permissionManage',
          component: PermissionManage
        },
        {
          path: 'videoAudit',
          name: 'videoAudit',
          component: VideoAudit
        }
      ]
    },
    {
      path: '/personalCenter',
      name: 'personalCenter',
      component: PersonalCenter,
      children: [
        {
          path: 'personalInfo',
          name: 'personalInfo',
          component: PersonalInfoComponent
        },
        {
          path: 'videoManage',
          name: 'videoManage',
          component: VideoManageComponent
        },
        {
          path: 'videoTransProgress',
          name: 'videoTransProgress',
          component: VideoTransProgressComponent
        },
        {
          path: 'collectionManage',
          name: 'collectionManage',
          component: CollectionManageComponent
        }
      ]
    },
    {
      path: '/noAuth',
      name: 'noAuth',
      component: NoAuth
    }
  ]
})

router.beforeEach((to, from, next) => {
  console.log("进入守卫");
  if (to.path === '/login' || to.path == '/signUp' || to.path == '/noAuth') { // 当路由为login时就直接下一步操作
    next();
  } else { // 否则就需要判断
    if (cookies.get("userName") == null) { // 没有用户名就跳转到login页面
      console.log("返回登录页");
      next({
        path: '/login',
        replace: true
      }) // 如果有用户名就进行下一步操作
    } else {
      console.log(cookies.get("userName"));
      console.log("已经登录");
      next()
    }
  }
})

export default router;
